﻿using MetaVideoEditorEvolved.Helper;

/*
 * Created by SharpDevelop.
 * User: fred
 * Date: 14/05/2011
 * Time: 11:39
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

namespace mveEngine
{
	using System;
	using System.IO;
	using System.Windows.Media.Imaging;
	
	/// <summary>
	/// Description of BitmapMetadataHelper.
	/// </summary>
	public static class BitmapMetadataHelper
	{
		public static string ReadMetadata(string fileName, TypeBitmapMetadata typeMetadata)
		{
			try
			{
				if(File.Exists(fileName))
				{
					using (Stream jpegStreamIn = File.Open(fileName, FileMode.Open, FileAccess.Read))
					{
						var decoder = new JpegBitmapDecoder(jpegStreamIn, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
						BitmapFrame frame = decoder.Frames[0];
						BitmapMetadata bmd = (BitmapMetadata)frame.Metadata;
						
						switch (typeMetadata) {
							case mveEngine.TypeBitmapMetadata.UniqueId:
								var valueuniqueid = bmd.GetQuery("/app1/ifd/exif:{uint=" + (int)TypeBitmapMetadata.UniqueId + "}");
								if(valueuniqueid == null)
								{
									return string.Empty;
								}
								else
								{
									return valueuniqueid.ToString();
								}
							default:
								throw new Exception("Invalid value for TypeBitmapMetadata");
						}
					}
				}
				
				return string.Empty;
			}
			catch
			{
				return string.Empty;
			}
		}
		
		public static void WriteMetadata(string fileName, TypeBitmapMetadata typeMetadata, string metadataValue)
		{
			try
			{
				JpegBitmapDecoder decoder;
				
				using (Stream jpegStreamIn = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
				{
					decoder = new JpegBitmapDecoder(jpegStreamIn, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
				}
				
				var bitmapFrame = decoder.Frames[0];
				
				if (bitmapFrame != null)
				{
					InPlaceBitmapMetadataWriter writer = bitmapFrame.CreateInPlaceBitmapMetadataWriter();
					writer.SetQuery("/app1/ifd/exif:{uint=306}", "2001:01:01 01:01:01");
					if (!writer.TrySave() == true)
					{
						BitmapMetadata metaData = (BitmapMetadata)bitmapFrame.Metadata.Clone();
						if (metaData != null)
						{
							switch (typeMetadata) {
								case mveEngine.TypeBitmapMetadata.UniqueId:
									metaData.SetQuery("/app1/ifd/exif:{uint=" + (int)TypeBitmapMetadata.UniqueId + "}", metadataValue);
									break;
								default:
									throw new Exception("Invalid value for TypeBitmapMetadata");
							}

							JpegBitmapEncoder encoder = new JpegBitmapEncoder();
							encoder.Frames.Add(BitmapFrame.Create(bitmapFrame, bitmapFrame.Thumbnail, metaData, bitmapFrame.ColorContexts));
							
							using (Stream jpegStreamOut = File.Open(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
							{
								encoder.Save(jpegStreamOut);
							}
						}
					}
				}
			}
			catch(Exception ex)
			{
				Tracer.TracerInstance.TraceError(string.Format("Can't write metadata in image : {0}, the exception is : {1}", fileName, ex.ToString()));
			}
		}
	}
	
	public enum TypeBitmapMetadata
	{
		UniqueId = 42016
	}
}
